<?php
namespace app\admin\controller;

use app\admin\tool\Parser;
use think\Exception;
use think\Request;
use app\common\model\Styles as StylesModel;
use app\common\model\Brands as BrandsModel;
use think\Db;

/**
 * 跟新平台的数据操作界面
 * @package app\admin\controller
 */
class Data extends Base {

    /**
     *
     * @return \think\response\View
     */
    public function index(){

        $this->data['title'] = '风格数据更新';
        $model = StylesModel::where([])->order('oid desc')->paginate(10);
        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'id'=>['name'=>'id','fun'=>'text','options'=>[]],
            'oid'=>['name'=>'平台原id','fun'=>'text','options'=>[]],
            'name'=>['name'=>'风格名称','fun'=>'text','options'=>[]],
            'introduce'=>['name'=>'风格描述','fun'=>'text','options'=>[]],
            'update_time'=>['name'=>'更新时间','fun'=>'text','options'=>[]],//update_time 系统自动转了 不需要自己转
        ];

        return view('',$this->data);
    }

    /**
     * 品牌数据
     */
    public function brand(){
        $this->data['title'] = '品牌数据更新';
        $model = BrandsModel::where([])->order('update_time desc')->paginate(10);

        $soidArr = [];
        foreach($model as $key => $val ){
            array_push($soidArr,$val['soid']);
        }
        $styles = StylesModel::where('oid','in',$soidArr)->column("oid,name",'oid');


        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'id'=>['name'=>'id','fun'=>'text','options'=>[]],
            'oid'=>['name'=>'平台原id','fun'=>'text','options'=>[]],
            'soid'=>['name'=>'所属风格','fun'=>'translate','options'=>['map'=>$styles]],
            'name'=>['name'=>'品牌名称','fun'=>'text','options'=>[]],
            'head_image'=>['name'=>'主图','fun'=>'singleImage','options'=>[]],
            'update_time'=>['name'=>'更新时间','fun'=>'text','options'=>[]],//update_time 系统自动转了 不需要自己转
        ];

        return view('',$this->data);
    }


    /**
     * 自营产品数据
     * @return \think\response\View
     */
    public function product(){
        $this->data['title'] = '产品数据更新';
        $model = BrandsModel::where([])->order('update_time desc')->paginate(10);

        $soidArr = [];
        foreach($model as $key => $val ){
            array_push($soidArr,$val['soid']);
        }
        $styles = StylesModel::where('oid','in',$soidArr)->column("oid,name",'oid');


        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'id'=>['name'=>'id','fun'=>'text','options'=>[]],
            'oid'=>['name'=>'平台原id','fun'=>'text','options'=>[]],
            'soid'=>['name'=>'所属风格','fun'=>'translate','options'=>['map'=>$styles]],
            'name'=>['name'=>'品牌名称','fun'=>'text','options'=>[]],
            'head_image'=>['name'=>'主图','fun'=>'singleImage','options'=>[]],
            'update_time'=>['name'=>'更新时间','fun'=>'text','options'=>[]],//update_time 系统自动转了 不需要自己转
        ];

        return view('',$this->data);
    }


    /**
     *
     */
    public function getStyles(){
        $model = StylesModel::where([])->paginate(10);
        $this->data['title'] = '风格列表';
        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'name'=>['name'=>'风格名称','fun'=>'text','options'=>''],
        ];
        return view('',$this->data);
    }


    /**
     * @param Request $request
     */
    public function getStyle(Request $request){
        $oid = $request->param('oid');
        $style = StylesModel::where(['oid'=>$oid])->find();
        if(empty($style)){
            $this->error("错误的操作");
        }
        $this->success('获取成功','',$style);
    }


    public function getBrands(Request $request){
        $soid = $request->param('oid');//这个是风格的平台id
        $model = BrandsModel::where(['soid'=>$soid])->paginate(10,false,['query'=>['oid'=>$soid]]);
        $this->data['title'] = '品牌列表';
        $this->data['model'] = $model;
        $this->data['parser'] = new Parser();
        $this->data['column'] = [
            'name'=>['name'=>'品牌名称','fun'=>'text','options'=>''],
        ];
        return view('',$this->data);
    }

    /**
     * @param Request $request
     */
    public function getBrand(Request $request){
        $oid = $request->param('oid');
        $brand = BrandsModel::where(['oid'=>$oid])->find();
        if(empty($brand)){
            $this->error("错误的操作");
        }
        $this->success('获取成功','',$brand);
    }

}